package com.zhiy.com.estation.ui.fragment;

import android.app.ProgressDialog;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.zhiy.com.estation.R;
import com.zhiy.com.estation.ui.base.BaseFragment;
import com.zhiy.com.estation.ui.fragment.login.LoginContract;
import com.zhiy.com.estation.ui.fragment.register.RegisterContract;
import com.zhiy.com.estation.ui.fragment.register.RegisterModel;
import com.zhiy.com.estation.ui.fragment.register.RegisterPresenter;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by Zyh on 2016/12/3.
 */
public class Fragment_register extends BaseFragment implements RegisterContract.View {
    @BindView(R.id.regist_info_et)
    EditText registInfoEt;
    private ProgressDialog mProgressDialog;

    @Override
    protected int getInflaterLayoutId() {
        return R.layout.fragment_register;
    }

    @Override
    protected void initView(View view) {
        mProgressDialog = new ProgressDialog(mBaseActivity);
        mProgressDialog.setMessage("正在注册");
    }

    private boolean isNext = true;
    private String nameInfo;//记录用户的用户名
    private String passInfo;//记录用户的密码
    @OnClick(R.id.regist_bt)
    public void btClick(Button view) {
        String info= registInfoEt.getText().toString();
        if (TextUtils.isEmpty(info)){
            Toast.makeText(mContext, "输入内容不能为空", Toast.LENGTH_SHORT).show();
        }else {
            if (isNext){
                nameInfo=info;
                view.setText("注册");
                registInfoEt.setText(null);
                registInfoEt.setHint("请输入密码");
                isNext=!isNext;
            }else {
                passInfo=info;
                registInfoEt.setText(null);
                registInfoEt.setHint("请输入用户名");
                view.setText(R.string.next);
                isNext=!isNext;
                register();
            }
        }
    }
    public void register(){
        RegisterPresenter presenter=new RegisterPresenter();
        presenter.setMV(new RegisterModel(),this);
        presenter.regist(nameInfo,passInfo);
    }
    @Override
    public void isRegisterSuccess() {
        mProgressDialog.dismiss();
    }

    @Override
    public void isFailure() {
        mProgressDialog.dismiss();
        Toast.makeText(mContext, "注册失败,请重新注册", Toast.LENGTH_LONG).show();
    }

    @Override
    public void showMsg(String msg) {
        Log.e("----", msg);
    }
}
